package com.sina.interfaces.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.hibernate.SessionFactory;
import org.hibernate.dialect.MySQL55Dialect;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * Created by 2017/9/7
 * Dao配置
 */
@Configuration
public class DaoConfig {

    @Resource
    private Environment env;

    @Bean(destroyMethod = "close")
    @Primary
    public DruidDataSource interfaceDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(env.getProperty("spring.datasource.url"));
        druidDataSource.setUsername(env.getProperty("spring.datasource.username"));
        druidDataSource.setPassword(env.getProperty("spring.datasource.password"));
        druidDataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        return druidDataSource;
    }

    @Bean
    public SessionFactory interfaceSessionFactory() {
        LocalSessionFactoryBuilder sessionFactoryBuilder = new LocalSessionFactoryBuilder(interfaceDataSource());
        sessionFactoryBuilder.scanPackages("com.sina.interfaces.domain");
        Properties properties = new Properties();
        properties.put("hibernate.dialect", MySQL55Dialect.class);
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.format_sql", "true");
        properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        properties.put("javax.persistence.validation.mode", "none");
        sessionFactoryBuilder.addProperties(properties);
        return sessionFactoryBuilder.buildSessionFactory();
    }

    @Bean
    public HibernateTemplate interfaceHibernateTemplate(SessionFactory interfaceSessionFactory) {
        HibernateTemplate hibernateTemplate = new HibernateTemplate();
        hibernateTemplate.setSessionFactory(interfaceSessionFactory);
        return hibernateTemplate;
    }

    @Bean
    public HibernateTemplate hibernateTemplate() {
        return new HibernateTemplate(interfaceSessionFactory());
    }

    @Primary
    @Bean("interfaceTx")
    public HibernateTransactionManager transactionManager(@Qualifier("interfaceDataSource") DataSource dataSource) {
        HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
        hibernateTransactionManager.setDataSource(dataSource);
        hibernateTransactionManager.setSessionFactory(interfaceSessionFactory());
        return hibernateTransactionManager;
    }

    @Bean
    public DataSourceTransactionManager interfaceDataSourceTx(@Qualifier("interfaceDataSource") DataSource dataSource) {
        DataSourceTransactionManager tx = new DataSourceTransactionManager(dataSource);
        tx.setEnforceReadOnly(true);
        return tx;
    }
}
